<?php
/**
 * Description of VideoFacade
 *
 * @author Alexey
 */
class VideoFacade {

    public static function loadModel($id)
    {
        $model=Video::model()->findByPk((int)$id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

    public static function saveModel($model)
    {
        $isNewVideo = $model->isNewRecord;
        $success = true;
        if ($isNewVideo)
        {
            $commentFlow = new CommentFlow;
            $success &= $commentFlow->save();
            $model->comment_flow_id = $commentFlow->comment_flow_id;
        }
        $success &= $model->save();
        if ($success && $isNewVideo)
        {
            $userVideo = new UserVideo;
            $userVideo->user_id = Yii::app()->controller->getUserIdFromSession();
            $userVideo->video_id = $model->video_id;
            $success &= $userVideo->save();
        }
        return $success;
    }

    public static function isAuthor(Video $video)
    {
        $userId = Yii::app()->controller->getUserIdFromSession();
        $userVideo = UserVideo::model()->find('user_id=:userId AND video_id=:videoId',
                    array(':userId' => $userId, ':videoId' => $video->video_id));
        if ($userVideo)
        {
            return true;
        }
        return false;
    }

    public static function getAuthorUser($video)
    {
        $userVideo = UserVideo::model()->find('video_id=:videoId',
                    array(':videoId' => $video->video_id));

        $user = UserFacade::getUserById($userVideo->user_id);
        return $user;
    }

    private static function getVideoDBCriteriaByUserId($userId)
    {
        if (empty($userId))
        {
            throw new CException("UserId can not be null.");
        }
        $criteria = new CDbCriteria();
        $criteria->condition = 'video_id IN (SELECT uservideo.video_id FROM uservideo WHERE user_id = '. $userId .') ';
        $criteria->order = "publish_date DESC";
        return $criteria;
    }

    public static function getDataProviderByUser($user)
    {
        $dataProvider = new CActiveDataProvider( 'Video', array(
            'criteria'=>VideoFacade::getVideoDBCriteriaByUserId( $user->user_id )
        ));

        return $dataProvider;

    }

    public static function getLatesUserVideo($userId, $count = 3)
    {
        $criteria = VideoFacade::getVideoDBCriteriaByUserId( $userId );
        $criteria->limit = $count;
        $video = Video::model()->findAll($criteria);
        return $video;
    }

    public static function getVideoCountByUser($userId)
    {
        $criteria = VideoFacade::getVideoDBCriteriaByUserId( $userId );
        return Video::model()->count($criteria);
    }

    public static function getVideoYoutubeCodeByUrl($url)
    {
        $arrUrl = parse_url($url);
        $arrQuery = parse_str($arrUrl['query']);
        return $v;
    }

    public static function deleteModel($id)
    {
        $video = VideoFacade::loadModel( $id );
        $commentFlowId = $video->comment_flow_id;
        $video->comment_flow_id = NULL;
        $video->save();
        CommentFacade::deleteCommentFlowModel($commentFlowId);
        $video->delete();
    }

    public static function getDataProviderForSearch($title)
    {
        $criteria = new CDbCriteria();
        if (!empty($title ))
            $criteria->addSearchCondition( 'title', $title, true, 'OR' );

        $dataProvider = new CActiveDataProvider( 'Video', array( 'criteria' => $criteria, ) );
        return $dataProvider;
    }
}
?>
